﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.Practices.Unity;
using EPlanet.Web.UI.WebControls;
using System.Data;
using DreamBuilder.Common;
using DreamBuilder.Core.Message;
using CommonModule;
using Web.SystemConfig.Services;
using DreamBuilder.Core.Container;
using DreamBuilder.Core.Log;
using Web.SystemLogin.Business;
using System.Threading;


namespace Web.Views.SystemConfig
{
    public partial class P_BT_PositionEdit : PageBase
    {
        #region 控件及变量定义
        /// <summary>
        /// Business类
        /// </summary>
        [Dependency]
        public IBusiness Business
        {
            get;
            set;
        }
        [Dependency]
        public IBusiness BUSCom
        {
            get;
            set;
        }
        [Dependency]
        public IAuth_User Auth_UserComponent
        {
            get;
            set;
        }
        #endregion

        #region PageLoad方法

        protected void Page_Load(object sender, System.EventArgs e)
        {try{
            SetPageProperty();
            InitControlsCultureInfo();
            if (!IsPostBack)
            {
                ShowEdit();
            }
            CreateToolMenu(Toolbar1, ToolMenuType.TYPE_Edit);
            base.CheckEditAuthorities(base.CurrentPageState);
        }
        catch (Exception ex)
        {
            WriteErrorLog(ex.Message);
            ExceptionMsg();
        }
        }

        /// <summary>
        /// 设置页面的菜单属性
        /// </summary>
        private void SetPageProperty()
        {
            this.DBHelpName = DBHelperString.Dictionary;
            base.MenuID = "ePlanet.Configuration.Position";
            if (!IsPostBack)
            {
                if ((Request["pkValue"] == null || Request["pkValue"].Trim().Length == 0))
                {
                    base.CurrentPageState = PageState.State_New;
                }
                else
                {
                    base.CurrentPageState = PageState.State_Edit;
                    base.PKValue = Request.QueryString.Get("pkValue");
                }
            }
        }

        /// <summary>
        /// 显示列表,为列表控件的单据id赋值
        /// </summary>
        private void ShowEdit()
        {
            ucedit.VoucherID = "vch_P_BT_Position";
            ucedit.PKValue = base.PKValue;
        }

        /// <summary>
        /// 初始化控件多语信息
        /// </summary>
        private void InitControlsCultureInfo()
        {
            this.TabControl1.Items[0].Caption = GetString("Position");
        }

        #endregion

        #region OnPreRender方法

        /// <summary>
        /// 重写OnPreRender方法
        /// </summary>
        /// <param name="e"></param>
        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);
            //by liuyingnan
            SetControlEnabled();
        }
        private void SetControlEnabled()
        {

            //初始所属单位
            if (base.CurrentPageState == PageState.State_New)
            {
                string OrdercontrolID = this.ucedit.GetEditControlID("P_BT_Position", "Order_Num");
                if (!string.IsNullOrEmpty(OrdercontrolID))
                {
                    Control ordercnl = this.ucedit.FindControl(OrdercontrolID);
                    if (ordercnl is TextBox)
                    {
                        TextBox orderText = ordercnl as TextBox;
                        orderText.Text = (Auth_UserComponent.MaxPositionNum() + 1).ToString();
                    }
                    else
                    {
                        Response.Redirect("P_BT_Position.aspx");
                        //如果获取失败 跳转回主页面
                    }
                }
            }
        }

        #endregion


        #region 事件处理方法
        /// <summary>
        /// 工具条事件处理
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Toolbar1_MenuItemClick(object sender, MenuItemClickEventArgs e)
        {
            switch (e.EventItem.ID.ToString().ToLower())
            {
                case "tbsave":
                    this.Validate();
                    if (this.IsValid)
                    {
                        OnSaveData();
                    }
                    break;
                case "tbcancel":
                    RedirectList();
                    break;
                case "tbhelp":
                    break;
            }
        }
        #endregion

        #region 保存
        /// <summary>
        /// 保存数据
        /// </summary>
        private void OnSaveData()
        {
            System.Data.DataSet dsInfo = new System.Data.DataSet();
            dsInfo.Tables.Add(ucedit.GetEditDataTable());
            if (dsInfo != null)
            {
                string userCheckName = dsInfo.Tables[0].Rows[0]["P_BT_Position.PositionName"].ToString();
                if (String.IsNullOrEmpty(Request.QueryString["pkValue"].ToString()))
                {
                    string userCheckOrder = dsInfo.Tables[0].Rows[0]["P_BT_Position.Order_Num"].ToString();                   
                    bool userCheckPosition = Auth_UserComponent.CheckPositionOrder(userCheckOrder);
                    if (userCheckPosition)
                    {
                        this.ShowMsg("此排列序号已存在，请更改排列序号。");
                        DisplayMessage("此排列序号已存在，请更改排列序号。");
                        return;
                    }
                    
                    bool userCheckPositionName = Auth_UserComponent.CheckPositionName(userCheckName);
                    if (userCheckPositionName)
                    {
                        this.ShowMsg("此职位名称已存在，请更改职位名称。");
                        DisplayMessage("此职位名称已存在，请更改职位名称。");
                        return;
                    }
                }
                //处理主表数据
                HandleMainTable(dsInfo.Tables[0]);
                ResultInfo result = BUSCom.Save(dsInfo);
                string mainKey = dsInfo.Tables[0].Rows[0][0].ToString();
                if (result.bSuccess == true)
                {
                    if ((Request["pkValue"] == null || Request["pkValue"].Trim().Length == 0))
                    {
                        Auth_UserComponent.ModifyUser("P_BT_Position", "Create_User", this.CurrentUser.User_ID, "Create_Time", DateTime.Now, "Position_ID", mainKey);
                        Auth_UserComponent.ModifyUser("P_BT_Position", "Modify_User", this.CurrentUser.User_ID, "Modify_Time", DateTime.Now, "Position_ID", mainKey);
                        string operationMessage = this.CurrentUser.Real_Name + "新增了" + userCheckName + "[" + mainKey + "]职务";
                        WriteUserLog(operationMessage,OracleLogType.System);
                    }
                    else
                    {
                        Auth_UserComponent.ModifyUser("P_BT_Position", "Modify_User", this.CurrentUser.User_ID, "Modify_Time", DateTime.Now, "Position_ID", mainKey);
                        string operationMessage = this.CurrentUser.Real_Name + "修改了" + userCheckName + "[" + mainKey + "]职务";
                        WriteUserLog(operationMessage,OracleLogType.System);
                    }
                    RedirectList();
                }
                else
                {
                    DisplayMessage(result.ErrorDesc);
                }
            }
        }

        /// <summary>
        /// 处理主表数据
        /// </summary>
        /// <param name="dataTable"></param>
        private void HandleMainTable(DataTable dataTable)
        {
        }


        #endregion

        #region 页面跳转
        /// <summary>
        /// 跳转到列表页面
        /// </summary>
        private void RedirectList()
        {
            Response.Redirect("P_BT_Position.aspx");
        }
        #endregion

        #region OnInit
        override protected void OnInit(EventArgs e)
        {
            //
            //CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
            //
            InitializeComponent();
            base.OnInit(e);
        }
        /// <summary>
        /// 添加页面及控件事件
        /// </summary>
        private void InitializeComponent()
        {
            this.Toolbar1.MenuItemClick += new EPlanet.Web.UI.WebControls.MenuItemClickedEventHandler(Toolbar1_MenuItemClick);
        }
        #endregion
    }
}
